package zengjin.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zengjin.common.PageResponse;
import zengjin.common.R;
import zengjin.model.dto.offer.AddOfferRequest;
import zengjin.model.dto.offer.PageOfferRequest;
import zengjin.model.dto.offer.UpdateOfferRequest;
import zengjin.model.vo.offer.OfferDetailVO;
import zengjin.model.vo.offer.PageOfferVO;
import zengjin.service.OfferService;

/**
 * @author 19324
 */
@Tag(name = "Offer相关接口")
@RestController
@RequestMapping("/offer")
@Slf4j
public class OfferController {

    @Resource
    private OfferService offerService;


    //1.添加Offer
    @Operation(summary = "添加Offer")
    @PostMapping("/addOffer")
    public R<Boolean> addOffer(@RequestBody AddOfferRequest addOfferRequest) {
        //1.参数校验
        if (addOfferRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Boolean result = offerService.addOffer(addOfferRequest);
        if(!result){
            throw new RuntimeException("添加Offer失败");
        }
        return R.success(true, "添加Offer成功");
    }


    //2.修改Offer状态
    @Operation(summary = "修改Offer状态")
    @PostMapping("/updateOfferStatus")
    public R<Boolean> updateOfferStatus(Long offerId, String offerStatus) {
        //1.参数校验
        if (offerId == null||offerStatus == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Boolean result = offerService.updateOfferStatus(offerId, offerStatus);
        if(!result){
            return R.error(false, "修改Offer状态失败");
        }
        return R.success(true, "修改Offer状态成功");
    }

    //3.修改Offer信息
    @Operation(summary = "修改Offer信息")
    @PostMapping("/updateOffer")
    public R<Boolean> updateOffer(@RequestBody UpdateOfferRequest updateOfferRequest) {
        //1.参数校验
        if (updateOfferRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Boolean result = offerService.updateOffer(updateOfferRequest);
        if(!result){
            return R.error(false, "修改Offer信息失败");
        }
        return R.success(true, "修改Offer信息成功");
    }

    //4.分页查询Offer信息
    @Operation(summary = "分页查询Offer信息")
    @PostMapping("/pageOffer")
    public R<PageResponse<PageOfferVO>> pageOffer(@RequestBody PageOfferRequest pageOfferRequest) {
        //1.参数校验
        if (pageOfferRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        PageResponse<PageOfferVO> result = offerService.pageOffer(pageOfferRequest);
        if(result == null){
            return R.error(null, "分页查询Offer信息失败");
        }
        return R.success(result, "分页查询Offer信息成功");
    }

    //5.查询Offer信息根据offerId
    @Operation(summary = "查询Offer信息根据offerId")
    @PostMapping("/getOfferDetailById")
    public R<OfferDetailVO> getOfferDetailById(Long offerId) {
        //1.参数校验
        if (offerId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        OfferDetailVO result = offerService.getOfferDetailById(offerId);
        if(result == null){
            return R.error(null, "查询Offer信息失败");
        }
        return R.success(result, "查询Offer信息成功");
    }

}
